<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../static/static/css/font.css">
    <link rel="stylesheet" href="../static/static/css/weadmin.css">
    <script src="../static/lib/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="weadmin-body">
    <form class="layui-form" onsubmit="return false;">
        <div class="layui-row">
            <div class="layui-inline">
                <input class="layui-input" placeholder="发货地" name="warehouseAddressName" id="warehouseAddressName">
            </div>
            <div class="layui-inline">
                <input class="layui-input" placeholder="收货地" name="name" id="name">
            </div>
            <button class="layui-btn" onclick="getNewData()"><i class="layui-icon">&#xe615;</i></button>
            <button class="layui-btn" onclick="changePrice()"><i class="layui-icon"></i>修改运费
            </button>
        </div>
    </form>
    <table id="list" lay-filter="test"></table>
</div>
<script>
    var tableIns;
    layui.use('table', function () {
        //执行渲染
        tableIns = layui.table.render({
            elem: '#list'
            ,id: 'list'
            ,page: true
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'name', title: '收货地', width:100}
                ,{field: 'firstPrice', title: '首重/kg', width:200}
                ,{field: 'addPrice', title: '续重', width:200}
                ,{field: 'warehouseAddressName', title: '发货地', width:100}
            ]]
            , toolbar: true
            , url: '/shopFreight/getFreightTempletList'
            , method: 'post'
            , contentType: 'application/json'
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.status, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.infos //解析数据列表
                };
            }
        });

        layui.table.on('tool(test)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if(layEvent === 'detail'){

            } else if(layEvent === 'del'){

            } else if(layEvent === 'edit'){

            }
        });
    });
    var getNewData = function () {
        tableIns.reload({
            where: {
                warehouseAddressName: document.getElementById("warehouseAddressName").value,
                name: document.getElementById("name").value
            }
            ,page: {
                curr: 1
            }
        });
    }
    var changePrice = function () {
        var table = layui.table;
        var checkStatus = table.checkStatus('list')
            ,data = checkStatus.data;
        if (data.length>0){
            var ids=new Array(data.length)
            for (var i=0;i<data.length;i++){
                ids[i] = data[i].id;
          }
            var html = '<div class="layui-row">'+
                '    <div class="layui-input-block">' +
                '      <input type="text" id ="firstPrice" name="firstPrice" required  lay-verify="required" ' +
                'placeholder="请输入首重价格" autocomplete="off" class="layui-input">' +
                '    </div>' +
                '    <div class="layui-input-block">' +
                '      <input type="text" id ="addPrice" name="addPrice" required  lay-verify="required" ' +
                'placeholder="请输入续重价格" autocomplete="off" class="layui-input">' +
                '    </div>'+'    </div>'
            var alert = layer.open({
                title: '批量修改运费'
                , content: html
                , yes: function (index, layero) {
                    var $ = layui.$;
                    var loadIng = layer.load(2);
                    var model = {
                            firstPrice: document.getElementById("firstPrice").value,
                            addPrice: document.getElementById("addPrice").value,
                            ids: ids
                    }
                    $.ajax({
                        type: "POST",
                        url: '/shopFreight/updateFreightTemplet',
                        data: JSON.stringify(model),
                        dataType: "json",
                        contentType: 'application/json',
                        success: function (data) {
                            if (data.status != 0) {
                                layer.close(loadIng);
                                layer.open({
                                    title: '提示'
                                    , content: data.msg
                                });
                            } else {
                                layer.close(loadIng);
                                layer.close(alert);
                                getNewData();
                            }
                        }
                    });
                }
            });
        }else {
            layer.open({
                title: '提示'
                , content: '请先勾选数据'
            });
        }
    }
</script>
<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">禁用</a>
</script>
</body>
</html>